package SwingView.Quiz;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

import java.awt.GridBagLayout;

import javax.swing.JLabel;

import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.ButtonGroup;
import javax.swing.JTextArea;
import javax.swing.JRadioButton;

import SwingModel.CmsQuizQuestion;

import java.awt.Font;
import java.util.ArrayList;
import java.util.List;

public class QuestionPanel4View extends JPanel{

	private ButtonGroup buttonGroup;
	private List<JRadioButton> listOfOption;
	private CmsQuizQuestion question;

	public QuestionPanel4View(CmsQuizQuestion question, int questionIndex) {
		
		this.question = question;
		
		setOpaque(true);
		setForeground(new Color(240,240,240));
		setBackground(Color.WHITE);
		GridBagLayout gridBagLayout = new GridBagLayout();
		setLayout(gridBagLayout);
		
		JLabel jlQuestion = new JLabel("Question " + questionIndex + ":");
		jlQuestion.setFont(new Font("Lucida Grande", Font.BOLD, 15));
		GridBagConstraints gbc_jlQuestion = new GridBagConstraints();
		gbc_jlQuestion.anchor = GridBagConstraints.LINE_START;
		gbc_jlQuestion.fill = GridBagConstraints.HORIZONTAL;
		gbc_jlQuestion.insets = new Insets(5, 20, 5, 20);
		gbc_jlQuestion.gridx = 0;
		gbc_jlQuestion.gridy = 0;
		add(jlQuestion, gbc_jlQuestion);
		
		JTextArea jtfQuestion = new JTextArea(question.getQuestion());
		jtfQuestion.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		jtfQuestion.setWrapStyleWord(true);
		jtfQuestion.setLineWrap(true);
		jtfQuestion.setOpaque(false);
		jtfQuestion.setEditable(false);
		GridBagConstraints gbc_jtfQuestion = new GridBagConstraints();
		gbc_jtfQuestion.weightx = 1.0;
		gbc_jtfQuestion.anchor = GridBagConstraints.LINE_START;
		gbc_jtfQuestion.insets = new Insets(0, 20, 5, 20);
		gbc_jtfQuestion.fill = GridBagConstraints.HORIZONTAL;
		gbc_jtfQuestion.gridx = 0;
		gbc_jtfQuestion.gridy = 1;
		add(jtfQuestion, gbc_jtfQuestion);
		
		char optionIndex = 'A';
		int index = 0;
		buttonGroup = new ButtonGroup();
		listOfOption = new ArrayList();
		for(String option : question.getOptions()){
			if(option!=null){
				JRadioButton jrbOption = new JRadioButton("" + optionIndex + ")  " + option);
				GridBagConstraints gbc_rdbtnNewRadioButton = new GridBagConstraints();
				gbc_rdbtnNewRadioButton.anchor = GridBagConstraints.LINE_START;
				gbc_rdbtnNewRadioButton.insets = new Insets(0, 30, 5, 30);
				gbc_rdbtnNewRadioButton.gridx = 0;
				gbc_rdbtnNewRadioButton.gridy = 2 + index;
				add(jrbOption, gbc_rdbtnNewRadioButton);
				jrbOption.setEnabled(false);
				if(question.getKey() == optionIndex) jrbOption.setSelected(true);
				listOfOption.add(jrbOption);
				buttonGroup.add(jrbOption);
				optionIndex++;
				index++;
			}
			else break;
		}
	}
	
	public int getQuestionID(){
		return question.getQuestionID();
	}

	@Override
	protected void paintComponent(Graphics g){
		super.paintComponent(g);
		g.fillRoundRect(0, 0, this.getWidth(), this.getHeight(), 10, 10);
	}
	
}
